
import string, sys, re

def c2r(_inf, _onf, _cdel):
    r'''
    Purpose: Switch columns to rows.
    Warning: Works (for now) for reasonably big files only. Don't push it.
    Syntax:  c2r.py <file name> "<delimiter>"
    '''
    lines = open(_inf, 'r').readlines()
    ofl = open(_onf, 'w')

    res = ""
    matrix = []

    try:
        for index, line in enumerate(lines):
            line = re.sub('\n', '', line)
            cols = string.split(line, _cdel)
            if line <> '':
                matrix.append(cols)

        res = zip(*matrix)

        for l in res:
            ofl.write(string.join(l, _cdel)+'\n')
        ofl.close()

    except:
        print 'Some error!'

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print c2r.__doc__
    else:
        c2r(sys.argv[1], sys.argv[1] + '_c2r_output.txt', sys.argv[2])

"""
# zip function demo:

table = [ ('Person', 'Disks', 'Books'),
          ('Zoe'   ,     12,      24 ),
          ('John'  ,     17,       5 ),
          ('Julien',      3,      11 )  ]         

print zip(*table)

>>>     [ ('Person', 'Zoe', 'John', 'Julien'),
          ('Disks' ,   12,     17,        3 ),
          ('Books' ,   24,      5,       11 ) ]
"""
